home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / src / general.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-12  |  19.0 KB  |  887 lines

  1. /* general.c -- Stuff that is used by all files. */
  2.  
  3. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7. Bash is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with Bash; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <sys/types.h>
  24. #include <sys/param.h>
  25.  
  26. #include <fcntl.h>
  27.  
  28. #include "shell.h"
  29. #if defined (USG)
  30. #include <string.h>
  31. #else
  32. #include <strings.h>
  33. #endif /* USG */
  34.  
  35. #if !defined (USG) || defined (HAVE_RESOURCE)
  36. #include <sys/time.h>
  37. #endif
  38.  
  39. #include <sys/times.h>
  40.  
  41. #ifndef NULL
  42. #define NULL 0x0
  43. #endif
  44.  
  45. extern int errno;
  46.  
  47. /* **************************************************************** */
  48. /*                                    */
  49. /*           Memory Allocation and Deallocation.            */
  50. /*                                    */
  51. /* **************************************************************** */
  52.  
  53. char *
  54. xmalloc (size)
  55. long size;
  56. {
  57.   register char *temp = (char *)malloc (size);
  58.  
  59.   if (!temp)
  60.     fatal_error ("Out of virtual memory!");
  61.  
  62.   return (temp);
  63. }
  64.  
  65. char *
  66. xrealloc (pointer, size)
  67.      register char *pointer;
  68. long size;
  69. {
  70.   char *temp;
  71.  
  72.   if (!pointer)
  73.     temp = (char *)xmalloc (size);
  74.   else
  75.     temp = (char *)realloc (pointer, size);
  76.  
  77.   if (!temp)
  78.     fatal_error ("Out of virtual memory!");
  79.  
  80.   return (temp);
  81. }
  82.  
  83.  
  84. /* **************************************************************** */
  85. /*                                    */
  86. /*             Integer to String Conversion            */
  87. /*                                    */
  88. /* **************************************************************** */
  89.  
  90. /* Number of characters that can appear in a string representation
  91.    of an integer.  32 is larger than the string rep of 2^^31 - 1. */
  92. #define MAX_INT_LEN 32
  93.  
  94. /* Integer to string conversion.  This conses the string; the
  95.    caller should free it. */
  96. char *
  97. itos (i)
  98.      int i;
  99. {
  100.   char *buf, *p, *ret;
  101.   int negative = 0;
  102.   unsigned int ui;
  103.  
  104.   buf = xmalloc (MAX_INT_LEN);
  105.  
  106.   if (i < 0)
  107.     {
  108.       negative++;
  109.       i = -i;
  110.     }
  111.  
  112.   ui = (unsigned int) i;
  113.  
  114.   buf[MAX_INT_LEN - 1] = '\0';
  115.   p = &buf[MAX_INT_LEN - 2];
  116.  
  117.   do
  118.     *p-- = (ui % 10) + '0';
  119.   while (ui /= 10);
  120.  
  121.   if (negative)
  122.     *p-- = '-';
  123.  
  124.   ret = savestring (p + 1);
  125.   free (buf);
  126.   return (ret);
  127. }
  128.  
  129. /* A function to unset no-delay mode on a file descriptor.  Used in shell.c
  130.    to unset it on the fd passed as stdin.  Should be called on stdin if
  131.    readline gets an EAGAIN or EWOULDBLOCK when trying to read input. */
  132.  
  133. #if !defined (O_NDELAY)
  134. #  if defined (FNDELAY)
  135. #    define O_NDELAY FNDELAY
  136. #  endif
  137. #endif /* O_NDELAY */
  138.  
  139. /* Make sure no-delay mode is not set on file descriptor FD. */
  140. void
  141. unset_nodelay_mode (fd)
  142.      int fd;
  143. {
  144.  
  145. /**
  146.  ** (sjk)++ We remove the call to make stdin a noblocking stream, since this 
  147.             is not a feature of our limited fcntl() call. No blocking Input 
  148.         is however taken care of by the console_input_status()
  149.             and console_read_byte() pair in the readline source code.
  150.  **/
  151. #if !defined(atarist)
  152.   int flags, set = 0;
  153.  
  154.   if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
  155.     return;
  156.  
  157. #if defined (O_NONBLOCK)
  158.   if (flags & O_NONBLOCK)
  159.     {
  160.       flags &= ~O_NONBLOCK;
  161.       set++;
  162.     }
  163. #endif /* O_NONBLOCK */
  164.  
  165. #if defined (O_NDELAY)
  166.   if (flags & O_NDELAY)
  167.     {
  168.       flags &= ~O_NDELAY;
  169.       set++;
  170.     }
  171. #endif /* O_NDELAY */
  172.  
  173.   if (set)
  174.     fcntl (fd, F_SETFL, flags);
  175. #endif 
  176. }
  177.  
  178.  
  179. /* **************************************************************** */
  180. /*                                    */
  181. /*            Generic List Functions                */
  182. /*                                    */
  183. /* **************************************************************** */
  184.  
  185. /* Call FUNCTION on every member of LIST, a generic list. */
  186. map_over_list (list, function)
  187.      GENERIC_LIST *list;
  188.      Function *function;
  189. {
  190.   while (list) {
  191.     (*function) (list);
  192.     list = list->next;
  193.   }
  194. }
  195.  
  196. /* Call FUNCTION on every string in WORDS. */
  197. map_over_words (words, function)
  198.      WORD_LIST *words;
  199.      Function *function;
  200. {
  201.   while (words) {
  202.     (*function)(words->word->word);
  203.     words = words->next;
  204.   }
  205. }
  206.  
  207. /* Reverse the chain of structures in LIST.  Output the new head
  208.    of the chain.  You should always assign the output value of this
  209.    function to something, or you will lose the chain. */
  210. GENERIC_LIST *
  211. reverse_list (list)
  212.      register GENERIC_LIST *list;
  213. {
  214.   register GENERIC_LIST *next, *prev = (GENERIC_LIST *)NULL;
  215.  
  216.   while (list) {
  217.     next = list->next;
  218.     list->next = prev;
  219.     prev = list;
  220.     list = next;
  221.   }
  222.   return (prev);
  223. }
  224.  
  225. /* Return the number of elements in LIST, a generic list. */
  226. list_length (list)
  227.      register GENERIC_LIST *list;
  228. {
  229.   register int i;
  230.  
  231.   for (i = 0; list; list = list->next, i++);
  232.   return (i);
  233. }
  234.  
  235. /* Delete the element of LIST which satisfies the predicate function COMPARER.
  236.    Returns the element that was deleted, so you can dispose of it, or -1 if
  237.    the element wasn't found.  COMPARER is called with the list element and
  238.    then ARG.  Note that LIST contains the address of a variable which points
  239.    to the list.  You might call this function like this:
  240.  
  241.    SHELL_VAR *elt = delete_element (&variable_list, check_var_has_name, "foo");
  242.    dispose_variable (elt);
  243. */
  244. GENERIC_LIST *
  245. delete_element (list, comparer, arg)
  246.      GENERIC_LIST **list;
  247.      Function *comparer;
  248. {
  249.   register GENERIC_LIST *prev = (GENERIC_LIST *)NULL;
  250.   register GENERIC_LIST *temp = *list;
  251.  
  252.   while (temp) {
  253.     if ((*comparer) (temp, arg)) {
  254.       if (prev) prev->next = temp->next;
  255.       else *list = temp->next;
  256.       return (temp);
  257.     }
  258.     prev = temp;
  259.     temp = temp->next;
  260.   }
  261.   return ((GENERIC_LIST *)-1);
  262. }
  263.  
  264. /* Find NAME in ARRAY.  Return the index of NAME, or -1 if not present.
  265.    ARRAY shoudl be NULL terminated. */
  266. find_name_in_list (name, array)
  267.      char *name, *array[];
  268. {
  269.   int i;
  270.  
  271.   for (i=0; array[i]; i++)
  272.     if (strcmp (name, array[i]) == 0)
  273.       return (i);
  274.  
  275.   return (-1);
  276. }
  277.  
  278. /* Return the length of ARRAY, a NULL terminated array of char *. */
  279. array_len (array)
  280.      register char **array;
  281. {
  282.   register int i;
  283.   for (i=0; array[i]; i++);
  284.   return (i);
  285. }
  286.  
  287. /* Free the contents of ARRAY, a NULL terminated array of char *. */
  288. free_array (array)
  289.      register char **array;
  290. {
  291.   register int i = 0;
  292.  
  293.   if (!array) return;
  294.  
  295.   while (array[i])
  296.     free (array[i++]);
  297.   free (array);
  298. }
  299.  
  300. /* Append LIST2 to LIST1.  Return the header of the list. */
  301. GENERIC_LIST *
  302. list_append (head, tail)
  303.      GENERIC_LIST *head, *tail;
  304. {
  305.   register GENERIC_LIST *t_head = head;
  306.  
  307.   if (!t_head)
  308.     return (tail);
  309.  
  310.   while (t_head->next) t_head = t_head->next;
  311.   t_head->next = tail;
  312.   return (head);
  313. }
  314.  
  315. /* Some random string stuff. */
  316.  
  317. /* Remove all leading whitespace from STRING.  This includes
  318.    newlines.  STRING should be terminated with a zero. */
  319. strip_leading (string)
  320.      char *string;
  321. {
  322.   char *start = string;
  323.  
  324.   while (*string && (whitespace (*string) || *string == '\n')) string++;
  325.  
  326.   if (string != start)
  327.     {
  328.       int len = strlen (string);
  329.       bcopy (string, start, len);
  330.       start[len] = '\0';
  331.     }
  332. }
  333.  
  334. /* Remove all trailing whitespace from STRING.  This includes
  335.    newlines.  If NEWLINES_ONLY is non-zero, only trailing newlines
  336.    are removed.  STRING should be terminated with a zero. */
  337. strip_trailing (string, newlines_only)
  338.      char *string;
  339.      int newlines_only;
  340. {
  341.   int len = strlen (string) - 1;
  342.  
  343.   while (len >= 0)
  344.     {
  345.       if ((newlines_only && string[len] == '\n') ||
  346.           (!newlines_only && whitespace (string[len])))
  347.         len--;
  348.       else
  349.         break;
  350.     }
  351.   string[len + 1] = '\0';
  352. }
  353.  
  354. /* Turn STRING (a pathname) into an absolute pathname, assuming that
  355.    DOT_PATH contains the symbolic location of '.'.  This always
  356.    returns a new string, even if STRING was an absolute pathname to
  357.    begin with. */
  358.  
  359. #ifndef MAXPATHLEN
  360.  
  361. /**
  362.  ** (sjk)++ Adjust the MAXPATHLEN on the Atari ST, note that this should be 
  363.  **         done in a more independent way (by including a standard header).
  364.  **/
  365. #if !defined(atarist)
  366. #define MAXPATHLEN 1024
  367. #else 
  368. #define MAXPATHLEN 128
  369. #endif
  370. #endif
  371.  
  372. static char current_path[MAXPATHLEN];
  373.  
  374. char *
  375. make_absolute (string, dot_path)
  376.      char *string, *dot_path;
  377. {
  378.   register char *cp;
  379.  
  380. /** 
  381.  ** (sjk)++ let the string "[a..z]:" pass through as is is a valid drive 
  382.  **         specification on the Atari ST.
  383.  **/
  384. #if defined(atarist)
  385.   if ((strlen(string)>1) && (string[1] == ':'))
  386.     return(savestring (string));
  387. #endif 
  388.  
  389.   if (!dot_path || *string == '/')
  390.     return (savestring (string));
  391.  
  392.   strcpy (current_path, dot_path);
  393.  
  394.   if (!current_path[0])
  395.     strcpy (current_path, "./");
  396.  
  397.   cp = current_path + (strlen (current_path) - 1);
  398.  
  399.   if (*cp++ != '/')
  400.     *cp++ = '/';
  401.  
  402.   *cp = '\0';
  403.  
  404.   while (*string)
  405.     {
  406.       if (*string == '.')
  407.     {
  408.       if (!string[1])
  409.         return (savestring (current_path));
  410.  
  411.       if (string[1] == '/')
  412.         {
  413.           string += 2;
  414.           continue;
  415.         }
  416.  
  417.       if (string[1] == '.' && (string[2] == '/' || !string[2]))
  418.         {
  419.           string += 2;
  420.  
  421.           if (*string)
  422.         string++;
  423.  
  424.           pathname_backup (current_path, 1);
  425.           cp = current_path + strlen (current_path);
  426.           continue;
  427.         }
  428.     }
  429.  
  430.       while (*string && *string != '/')
  431.     *cp++ = *string++;
  432.  
  433.       if (*string)
  434.     *cp++ = *string++;
  435.  
  436.       *cp = '\0';
  437.     }
  438.   return (savestring (current_path));
  439. }
  440.  
  441. /* Remove the last N directories from PATH.  Do not PATH blank.
  442.    PATH must contain enoung space for MAXPATHLEN characters. */
  443. pathname_backup (path, n)
  444.      char *path;
  445.      int n;
  446. {
  447.   register char *p;
  448.  
  449.   if (!*path)
  450.     return;
  451.  
  452.   p = path + (strlen (path) - 1);
  453.  
  454.   while (n--)
  455.     {
  456.       while (*p == '/' && p != path)
  457.     p--;
  458.  
  459.       while (*p != '/' && p != path)
  460.     p--;
  461.  
  462.       *++p = '\0';
  463.     }
  464. }
  465.  
  466. /* Return 1 if STRING contains an absolute pathname, else 0. */
  467. absolute_pathname (string)
  468.      char *string;
  469. {
  470.   if (!string || !*string)
  471.     return (0);
  472.  
  473.   if (*string == '/')
  474.     return (1);
  475.  
  476.   if (*string++ == '.')
  477.     {
  478.       if ((!*string) || *string == '/')
  479.     return (1);
  480.  
  481.       if (*string++ == '.')
  482.     if (!*string || *string == '/')
  483.       return (1);
  484.     }
  485.   return (0);
  486. }
  487.  
  488. /* Return 1 if STRING is an absolute program name; it is absolute if it
  489.    contains any slashes.  This is used to decide whether or not to look
  490.    up through $PATH. */
  491. absolute_program (string)
  492.      char *string;
  493. {
  494.   return (index (string, '/') != NULL);
  495. }
  496.  
  497. /* Return the `basename' of the pathname in STRING (the stuff after the
  498.    last '/').  If STRING is not a full pathname, simply return it. */
  499. char *
  500. base_pathname (string)
  501.      char *string;
  502. {
  503.   char *p = rindex (string, '/');
  504.  
  505.   if (!absolute_pathname(string))
  506.     return (string);
  507.  
  508.   if (p)
  509.     return (++p);
  510.   else
  511.     return (string);
  512. }
  513.  
  514. /* Determine if s2 occurs in s1.  If so, return a pointer to the
  515.    match in s1.  The compare is case insensitive. */
  516. char *
  517. strindex (s1, s2)
  518.      register char *s1, *s2;
  519. {
  520.   register int i, l = strlen (s2);
  521.   register int len = strlen (s1);
  522.  
  523.   for (i = 0; (len - i) >= l; i++)
  524.     if (strnicmp (&s1[i], s2, l) == 0)
  525.       return (s1 + i);
  526.   return ((char *)NULL);
  527. }
  528.  
  529. #if !defined (to_upper)
  530. #define lowercase_p(c) (((c) > ('a' - 1) && (c) < ('z' + 1)))
  531. #define uppercase_p(c) (((c) > ('A' - 1) && (c) < ('Z' + 1)))
  532. #define pure_alphabetic(c) (lowercase_p(c) || uppercase_p(c))
  533. #define to_upper(c) (lowercase_p(c) ? ((c) - 32) : (c))
  534. #define to_lower(c) (uppercase_p(c) ? ((c) + 32) : (c))
  535. #endif /* to_upper */
  536.  
  537. /* Compare at most COUNT characters from string1 to string2.  Case
  538.    doesn't matter. */
  539. int
  540. strnicmp (string1, string2, count)
  541.      char *string1, *string2;
  542. {
  543.   register char ch1, ch2;
  544.  
  545.   while (count) {
  546.     ch1 = *string1++;
  547.     ch2 = *string2++;
  548.     if (to_upper(ch1) == to_upper(ch2))
  549.       count--;
  550.     else break;
  551.   }
  552.   return (count);
  553. }
  554.  
  555. /* strcmp (), but caseless. */
  556. int
  557. stricmp (string1, string2)
  558.      char *string1, *string2;
  559. {
  560.   register char ch1, ch2;
  561.  
  562.   while (*string1 && *string2) {
  563.     ch1 = *string1++;
  564.     ch2 = *string2++;
  565.     if (to_upper(ch1) != to_upper(ch2))
  566.       return (1);
  567.   }
  568.   return (*string1 | *string2);
  569. }
  570.  
  571. /* Return a string corresponding to the error number E.  From
  572.    the ANSI C spec. */
  573. #if defined (strerror)
  574. #undef strerror
  575. #endif
  576.  
  577. #if !defined (HAVE_STRERROR)
  578. char *
  579. strerror (e)
  580.      int e;
  581. {
  582.   extern int sys_nerr;
  583.   extern char *sys_errlist[];
  584.   static char emsg[40];
  585.  
  586.   if (e > 0 && e < sys_nerr)
  587.     return (sys_errlist[e]);
  588.   else
  589.     {
  590.       sprintf (emsg, "Unknown error %d", e);
  591.       return (&emsg[0]);
  592.     }
  593. }
  594. #endif /* HAVE_STRERROR */
  595.  
  596. #if !defined (USG) || defined (HAVE_RESOURCE)
  597. /* Print the contents of a struct timeval * in a standard way. */
  598. print_timeval (tvp)
  599.      struct timeval *tvp;
  600. {
  601.   int minutes, seconds_fraction;
  602.   long seconds;
  603.  
  604.   seconds = tvp->tv_sec;
  605.  
  606.   seconds_fraction = tvp->tv_usec % 1000000;
  607.   seconds_fraction = (seconds_fraction * 100) / 1000000;
  608.  
  609.   minutes = seconds / 60;
  610.   seconds %= 60;
  611.  
  612.   printf ("%0dm%0d.%02ds",  minutes, seconds, seconds_fraction);
  613. }
  614. #endif
  615.  
  616. /* Print the time defined by a time_t (returned by the `times' and `time'
  617.    system calls) in a standard way.  This is scaled in terms of HZ, which
  618.    is what is returned by the `times' call. */
  619.  
  620. #if !defined (BrainDeath)
  621. #if !defined (HZ)
  622. #  if defined (USG)
  623. #    define HZ 100        /* From my Sys V.3.2 manual for times(2) */
  624. #  else
  625. #    define HZ 60        /* HZ is always 60 on BSD systems */
  626. #  endif /* USG */
  627. #endif /* HZ */
  628.  
  629. print_time_in_hz (t)
  630.   time_t t;
  631. {
  632.   int minutes, seconds_fraction;
  633.   long seconds;
  634.  
  635.   seconds_fraction = t % HZ;
  636.   seconds_fraction = (seconds_fraction * 100) / HZ;
  637.  
  638.   seconds = t / HZ;
  639.  
  640.   minutes = seconds / 60;
  641.   seconds %= 60;
  642.  
  643.   printf ("%0dm%0d.%02ds",  minutes, seconds, seconds_fraction);
  644. }
  645. #endif /* BrainDeath */
  646.  
  647. #if defined (NO_DUP2)
  648. /* Replacement for dup2 (), for those systems which either don't have it,
  649.    or supply one with broken behaviour. */
  650. dup2 (fd1, fd2)
  651.      int fd1, fd2;
  652. {
  653.   int saved_errno, r;
  654.  
  655.   if (fcntl (fd1, F_GETFL, 0) == -1)    /* fd1 is an invalid fd */
  656.     return (-1);
  657.  
  658.   if (fd2 < 0 || fd2 >= getdtablesize ())
  659.     {
  660.       errno = EBADF;
  661.       return (-1);
  662.     }
  663.  
  664.   if (fd1 == fd2)
  665.     return (0);
  666.  
  667.   saved_errno = errno;
  668.  
  669.   (void) close (fd2);
  670.   r = fcntl (fd1, F_DUPFD, fd2);
  671.  
  672.   if (r >= 0)
  673.     errno = saved_errno;
  674.   else
  675.     if (errno == EINVAL)
  676.       errno = EBADF;
  677.  
  678.   return (r);
  679. }
  680.  
  681. #endif /* NO_DUP2 */
  682.  
  683. /*
  684.  * Return the total number of available file descriptors.
  685.  *
  686.  * On some systems, like 4.2BSD and its descendents, there is a system call
  687.  * that returns the size of the descriptor table: getdtablesize().  There are
  688.  * lots of ways to emulate this on non-BSD systems.
  689.  *
  690.  * On System V.3, this can be obtained via a call to ulimit:
  691.  *    return (ulimit(4, 0L));
  692.  *
  693.  * On other System V systems, NOFILE is defined in /usr/include/sys/param.h
  694.  * (this is what we assume below), so we can simply use it:
  695.  *    return (NOFILE);
  696.  *
  697.  * On POSIX systems, there are specific functions for retrieving various
  698.  * configuration parameters:
  699.  *    return (sysconf(_SC_OPEN_MAX));
  700.  *
  701.  */
  702.  
  703. #if defined (USG) || defined (HPUX)
  704. int
  705. getdtablesize ()
  706. {
  707. #if defined (_POSIX_VERSION)
  708.   return (sysconf(_SC_OPEN_MAX));    /* Posix systems use sysconf */
  709. #else
  710. #  if defined (USGr3)
  711.   return (ulimit (4, 0L));    /* System V.3 systems use ulimit(4, 0L) */
  712. #  else
  713. #    if defined (NOFILE)    /* Other systems use NOFILE */
  714.   return (NOFILE);
  715. #    else
  716.   return (20);            /* XXX - traditional value is 20 */
  717. #    endif /* NOFILE */
  718. #  endif /* USGr3 */
  719. #endif /* _POSIX_VERSION */
  720. }
  721. #endif /* USG && !defined USGr4 */
  722.  
  723. #if defined (USG) && !defined (sgi)
  724.  
  725. #if !defined (RISC6000)
  726. bcopy(s,d,n) char *d,*s; { memcpy (d, s, n); }
  727. bzero(s,n) char *s; int n; { memset(s, '\0', n); }
  728. #endif /* RISC6000 */
  729.  
  730. char *index(s,c) char *s; { char *strchr(); return strchr(s,c); }
  731. char *rindex(s,c) char *s; { char *strrchr(); return strrchr(s,c); }
  732.  
  733. #if !defined (HAVE_GETWD)
  734. char *
  735. getwd (string)
  736.      char *string;
  737. {
  738.   extern char *getcwd ();
  739.   char *result;
  740.  
  741.   result = getcwd (string, MAXPATHLEN);
  742.   if (result == NULL)
  743.     strcpy (string, "getwd: cannot access parent directories");
  744.   return (result);
  745. }
  746. #endif /* !HAVE_GETWD */
  747.  
  748. #if !defined (HPUX)
  749. #include <sys/utsname.h>
  750. gethostname (name, namelen)
  751.      char *name;
  752.      int namelen;
  753. {
  754.   int i;
  755.   struct utsname uts;
  756.  
  757.   --namelen;
  758.  
  759.   uname (&uts);
  760.   i = strlen (uts.nodename) + 1;
  761.   strncpy (name, uts.nodename, i < namelen ? i : namelen);
  762.   name[namelen] = '\0';
  763.   return (0);
  764. }
  765. #endif /* !HPUX */
  766. #endif /* USG && !sgi */
  767.  
  768. #if defined (USG) || defined (_POSIX_VERSION)
  769. int
  770. sysv_getc (stream)
  771.      FILE *stream;
  772. {
  773.   int result;
  774.   char c;
  775.  
  776.   while (1)
  777.     {
  778.       result = read (fileno (stream), &c, sizeof (char));
  779.  
  780.       if (result == 0)
  781.     return (EOF);
  782.  
  783.       if (result == sizeof (char))
  784.     return (c);
  785.  
  786.       if (errno != EINTR)
  787.     return (EOF);
  788.     }
  789. }
  790.  
  791. /* USG and POSIX systems do not have killpg ().  But we use it in
  792.    jobs.c, nojobs.c and builtins.c. */
  793. #if !defined (_POSIX_VERSION)
  794. #define pid_t int
  795. #endif /* _POSIX_VERSION */
  796.  
  797. int
  798. killpg (pgrp, sig)
  799.      pid_t pgrp;
  800.      int sig;
  801. {
  802.   int result;
  803.  
  804.   result = kill (-pgrp, sig);
  805.   return (result);
  806. }
  807. #endif /* USG  || _POSIX_VERSION */
  808.  
  809. #if !defined (READLINE)
  810. /* Expand FILENAME if it begins with a tilde.  This always returns
  811.    a new string. */
  812. char *
  813. tilde_expand (filename)
  814.      char *filename;
  815. {
  816.   char *dirname = filename ? savestring (filename) : (char *)NULL;
  817.  
  818.   if (dirname && *dirname == '~')
  819.     {
  820.       char *temp_name;
  821.       if (!dirname[1] || dirname[1] == '/')
  822.     {
  823.       /* Prepend $HOME to the rest of the string. */
  824.       char *temp_home = (char *)getenv ("HOME");
  825.  
  826.       temp_name = (char *)alloca (1 + strlen (&dirname[1])
  827.                       + (temp_home? strlen (temp_home) : 0));
  828.       temp_name[0] = '\0';
  829.       if (temp_home)
  830.         strcpy (temp_name, temp_home);
  831.       strcat (temp_name, &dirname[1]);
  832.       free (dirname);
  833.       dirname = savestring (temp_name);
  834.     }
  835.       else
  836.     {
  837.       struct passwd *getpwnam (), *user_entry;
  838.       char *username = (char *)alloca (257);
  839.       int i, c;
  840.  
  841.       for (i = 1; c = dirname[i]; i++)
  842.         {
  843.           if (c == '/') break;
  844.           else username[i - 1] = c;
  845.         }
  846.       username[i - 1] = '\0';
  847.  
  848.       if (!(user_entry = getpwnam (username)))
  849.         {
  850.           /* If the calling program has a special syntax for
  851.          expanding tildes, and we couldn't find a standard
  852.          expansion, then let them try. */
  853.           if (rl_tilde_expander)
  854.         {
  855.           char *expansion;
  856.  
  857.           expansion = (char *)(*rl_tilde_expander) (username);
  858.  
  859.           if (expansion)
  860.             {
  861.               temp_name = (char *)alloca (1 + strlen (expansion)
  862.                           + strlen (&dirname[i]));
  863.               strcpy (temp_name, expansion);
  864.               strcat (temp_name, &dirname[i]);
  865.               free (expansion);
  866.               goto return_name;
  867.             }
  868.         }
  869.           /* We shouldn't report errors. */
  870.         }
  871.       else
  872.         {
  873.           temp_name = (char *)alloca (1 + strlen (user_entry->pw_dir)
  874.                       + strlen (&dirname[i]));
  875.           strcpy (temp_name, user_entry->pw_dir);
  876.           strcat (temp_name, &dirname[i]);
  877.         return_name:
  878.           free (dirname);
  879.           dirname = savestring (temp_name);
  880.         }
  881.       endpwent ();
  882.     }
  883.     }
  884.   return (dirname);
  885. }
  886. #endif /* !READLINE */
  887.